
package com.wyzz.hr.bo.impl;

import com.wyzz.hr.bo.IEmailtemplateBO;
import com.wyzz.hr.bo.TemplateService;
import com.wyzz.hr.dao.IEmailtemplateDAO;
import com.wyzz.hr.pojo.Emailtemplate;
import com.wyzz.hr.util.Environment;

import java.io.File;
import java.util.List;
import org.hibernate.criterion.*;


public class EmailtemplateBoImpl implements IEmailtemplateBO {

	public EmailtemplateBoImpl() {
		dao = null;
	}

	public void setTemplateService(TemplateService templateService) {
		this.templateService = templateService;
	}

	public IEmailtemplateDAO getDao() {
		return dao;
	}

	public void setDao(IEmailtemplateDAO dao) {
		this.dao = dao;
	}

	public boolean addEmailtemplate(Emailtemplate one) {
		try {
			dao.saveObject(one);
			String savePath = createIfNotExistTemplateFloder();
			templateService.createTemplate(one.getEtTitle(),
					(new StringBuilder()).append(savePath).append(one.getId())
							.append("-title.ftl").toString());
			templateService.createTemplate(one.getEtContent(),
					(new StringBuilder()).append(savePath).append(one.getId())
							.append("-body.ftl").toString());
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	private String createIfNotExistTemplateFloder() {
		String savePath = (new StringBuilder()).append(
				Environment.getFileHome()).append(
				templateService.getTemplateFilePath()).toString();
		File tmp = new File(savePath);
		if (!tmp.exists())
			tmp.mkdirs();
		return savePath;
	}

	public Emailtemplate loadEmailtemplate(Class name, String id) {
		Emailtemplate template = (Emailtemplate) dao.loadObject(name, id, null,
				new boolean[0]);
		if (template == null) {
			return null;
		} else {
			String templateNo = template.getEtTitleNo();
			String title = templateService
					.getTemplateContent((new StringBuilder())
							.append(templateNo).append("_title.ftl").toString());
			String content = templateService
					.getTemplateContent((new StringBuilder())
							.append(templateNo).append("_body.ftl").toString());
			template.setEtContent(content);
			template.setEtTitle(title);
			return template;
		}
	}

	/**
	 * @deprecated Method delEmailtemplate is deprecated
	 */

	public boolean delEmailtemplate(Class name, String id) {
		try {
			Emailtemplate type = (Emailtemplate) dao.loadObject(name, id, null,
					new boolean[0]);
			dao.deleteObject(type);
			String path = (new StringBuilder()).append(
					Environment.getFileHome()).append(
					templateService.getTemplateFilePath()).toString();
			templateService.deleteTemplate((new StringBuilder()).append(path)
					.append(type.getId()).append("_title.ftl").toString());
			templateService.deleteTemplate((new StringBuilder()).append(path)
					.append(type.getId()).append("_body.ftl").toString());
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public String updateEmailtemplate(Emailtemplate one) {
		if (one == null)
			return "\u6A21\u677F\u4E0D\u5B58\u5728\uFF01";
		Emailtemplate db = loadEmailtemplate(
				com.wyzz.hr.pojo.Emailtemplate.class, one.getId());
		if (db == null)
			return "\u6A21\u677F\u4E0D\u5B58\u5728\uFF01";
		db.setEtSendMode(one.getEtSendMode());
		db.setEtStatus(one.getEtStatus());
		dao.updateObject(db);
		String templateNo = one.getEtTitleNo();
		String titleError = templateService.modifyTemplateContent(
				(new StringBuilder()).append(templateNo).append("_title.ftl")
						.toString(), one.getEtTitle());
		String contentError = templateService.modifyTemplateContent(
				(new StringBuilder()).append(templateNo).append("_body.ftl")
						.toString(), one.getEtContent());
		StringBuffer errorBuffer = new StringBuffer();
		if (titleError != null)
			errorBuffer.append(titleError);
		if (contentError != null)
			errorBuffer.append(contentError);
		if (errorBuffer.length() == 0)
			return null;
		else
			return errorBuffer.toString();
	}

	public List getList() {
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Emailtemplate.class);
		detachedCriteria.addOrder(Order.asc(Emailtemplate.PROP_ET_TITLE_NO));
		List result = dao.findByCriteria(detachedCriteria);
		return result;
	}

	public Emailtemplate getEmailTemplateByNo(String templateNo) {
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(Emailtemplate.class);
		detachedCriteria.add(Restrictions.eq(Emailtemplate.PROP_ET_TITLE_NO,
				templateNo));
		List result = dao.findByCriteria(detachedCriteria);
		return result.isEmpty() ? null : (Emailtemplate) result.get(0);
	}

	private IEmailtemplateDAO dao;
	private TemplateService templateService;
}
